package com.example.viewtest.AdapterGroup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;

import java.util.List;

public class HeaderBottomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //item类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_BOTTOM = 2;
    public static final int TYPE_GAME = 3;
    public static final int TYPE_LANDSCAPE = 4;
    public static final int ITEM_TYPE_LOAD = 5;

    private List<Bean> mdata;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private int mHeaderCount=1;//头部View个数
    private int mBottomCount=1;//底部View个数

    public HeaderBottomAdapter(Context context, List<Bean> data) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mdata = data;
    }
    public boolean isHeaderView(int position){
        return mHeaderCount != 0 && position < mHeaderCount;
    }
    //判断当前item是否是FooterView
    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= (mHeaderCount + getContentItemCount());
    }

    //除头部底部view外的内容item数量
    private int getContentItemCount() {
        return mdata.size();
    }

    //判断当前item类型
    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();
        if (isHeaderView(position)) {
            //头部View
            return ITEM_TYPE_HEADER;
        }
//        else if (isBottomView(position)) {
//            //底部View
//            return ITEM_TYPE_BOTTOM;
//        }
        else if (position == getItemCount() - 1){
            //内容View
            return ITEM_TYPE_LOAD;
        }else{
            return ITEM_TYPE_CONTENT;
        }
    }
    //内容 ViewHolder     放入ViewHolder，绑定TextView控件
    public static class ContentViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;
        public ContentViewHolder(View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.rv_item_text);
            imageView=(ImageView) itemView.findViewById(R.id.rv_item_image);
        }
    }
    //头部 ViewHolder
    public static class HeaderViewHolder extends RecyclerView.ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
    //底部 ViewHolder
    public static class BottomViewHolder extends RecyclerView.ViewHolder {

        public BottomViewHolder(View itemView) {
            super(itemView);
        }
    }
    //加载更多
    public static class LoadMoreHolder extends RecyclerView.ViewHolder{
        public static final int LOAD_STATE_LOADING = 0;
        public static final int LOAD_STATE_RELOAD = 1;
        public static final int LOAD_STATE_NORMAL = 2;//显示成功

        private LinearLayout mloading;
        private TextView mreload;

        public LoadMoreHolder(View itemView){
            super(itemView);
            mloading = itemView.findViewById(R.id.loading);
            mreload = itemView.findViewById(R.id.reload);
        }

        public void update(int state){
            //重置控件状态
            mloading.setVisibility(View.GONE);
            mreload.setVisibility(View.GONE);

            switch (state){
                case LOAD_STATE_LOADING:
                    mloading.setVisibility(View.VISIBLE);
                    break;
                case LOAD_STATE_RELOAD:
                    mreload.setVisibility(View.VISIBLE);
                    break;
                case LOAD_STATE_NORMAL:
                    mloading.setVisibility(View.GONE);
                    mreload.setVisibility(View.GONE);
                    break;
                default:
            }
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_HEADER){
            //为什么attachToRoot作为boolean类型默认值不是false？
            return new HeaderViewHolder(mLayoutInflater.inflate(R.layout.rv_header,parent,false));
        }else if (viewType == ITEM_TYPE_CONTENT){
            return new ContentViewHolder(mLayoutInflater.inflate(R.layout.rv_item,parent,false));
        }else if (viewType == ITEM_TYPE_BOTTOM){
            return new BottomViewHolder(mLayoutInflater.inflate(R.layout.rv_footer,parent,false));
        }
        else if(viewType == ITEM_TYPE_LOAD){
            return new LoadMoreHolder(mLayoutInflater.inflate(R.layout.rv_loadmore,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder){

        }else if (holder instanceof ContentViewHolder){
            ((ContentViewHolder) holder).textView.setText(mdata.get(position - mHeaderCount).getName());
            ((ContentViewHolder) holder).imageView.setImageResource(mdata.get(position - mHeaderCount).getImageId());
        }else if (holder instanceof BottomViewHolder){

        }else if (position == getItemCount()-1){
            ((LoadMoreHolder) holder).update(LoadMoreHolder.LOAD_STATE_LOADING);
        }
    }

    @Override
    public int getItemCount() {
        if (mdata != null) {
            return mHeaderCount + getContentItemCount() + mBottomCount;
        }
        return 0;
    }
}
